﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
/// <summary>
/// Summary description for PairwiseAlignment
/// </summary>
public class PairwiseAlignmentDB
{
    public String databaseSequence { get; set; }
    public String querySequence { get; set; }
    public String similaritySequence { get; set; }

	
    public PairwiseAlignmentDB(String databaseSequence, String querySequence)
	{
		//Preenche sequencias
        Char[] db = databaseSequence.ToCharArray();
        Array.Reverse(db);
        Char[] qr = querySequence.ToCharArray();
        Array.Reverse(qr);

        this.databaseSequence = new string(db);
        this.querySequence = new string(qr);
        //Preenche SimilaritySequence
        this.similaritySequence = SimilarityBuilder(this.databaseSequence, this.querySequence);
	}
    /// <summary>
            /// Returns the sequence depicting similarity between the DatabaseSequence and the QuerySequence
            /// eg. ||||  |||  ||||||
            /// </summary>
    public String SimilarityBuilder(String databaseSequence, String querySequence)
    {
        StringBuilder similarity = new StringBuilder();
        Dictionary<char, bool>  badChars = new Dictionary<char, bool>();
        badChars.Add('n', true);
        badChars.Add('N', true);
        badChars.Add('X', true);
        badChars.Add('x', true);
        badChars.Add('-', true);

        for (int i = 0; i < databaseSequence.Length; i++)
        {
            if (badChars.ContainsKey(querySequence[i]) || badChars.ContainsKey(databaseSequence[i]))
            {
                similarity.Append(' ');
            }
            else similarity.Append(databaseSequence[i] == querySequence[i] ? '|' : '.');

        }



        return similarity.ToString();
    }


    /// <summary>
    /// Returns the alignment of two sequences in the form specified by the formater
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.Capacity = (databaseSequence.Length + 3) * 3;
        sb.Append(databaseSequence + Environment.NewLine);
        sb.Append(similaritySequence + Environment.NewLine);
        sb.Append(querySequence + Environment.NewLine);
        return sb.ToString();
    }
}